﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using pdtransport_v2._0.Models;
using pdtransport_v2._0.Areas.back.Models;
using System.Web.Routing;
using System.Web.Security;

namespace pdtransport_v2._0.Areas.back.Controllers
{
    public class AdminController : Controller
    {
        //
        // GET: /back/Admin/
        pdtransportDataContext datacontext = new pdtransportDataContext();
        HelpController help = new HelpController();

        public ActionResult Index()
        {
            return View();
        }

        [HttpGet]
        public ActionResult LogOn()
        {
            return View();
        }

        // Tham khảo
        [HttpPost]
        public ActionResult LogOn(string username, string password)
        {
            string encrypt_pass = help.Encrypt(password);
            int sotk = 0;
            sotk = (from _tk in datacontext.TaiKhoans
                    where _tk.username == username && _tk.password == encrypt_pass
                    select _tk).Count();
            if (sotk != 0) // có tài khoản
            {
                TaiKhoan tk = (from _tk in datacontext.TaiKhoans
                               where _tk.username == username && _tk.password == encrypt_pass
                               select _tk).Single();
                FormsAuthentication.SetAuthCookie(username, false); // không remember -> set false
                Session["username"] = username;
                Session["role"] = tk.VaiTro;
                return RedirectToAction("Index", "Home_back"); // trả về trang mà cả admin và manager đều ở đó được
               
            }

            // không có tài khoản
            ViewBag.Error = 1;
            return View("LogOn");
        }

        [HttpGet]
        public ActionResult LogOff()
        {
            FormsAuthentication.SignOut();
            return RedirectToAction("LogOn", "Admin");
        }
        // Hết tham khảo

    }
}
